<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 15/9/25
 * Time: 下午2:12
 */
class WxwMarketController extends BaseController {
    /*
     * test
     * */
    public function test(){
        $validator = Validator::make(Input::all(),
            array(
                'phone' => 'required',
//                'area' => 'required',
                'check_code' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.user_login_args_error_1011");
                break;
            }

            //传入参数
            $phone = Input::get('phone');

        } while (false);
        Log::info("info", array("system" => "wxw" ,"event" => "test", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 广告信息接口
     * */
    public function ads(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'type' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.user_login_args_error_1011");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $type = Input::get('type');
            $city = Input::get('city')?Input::get('city'):1;
            $now = Common::num2DateTime(time());
            //验证token
            $user_data = Users::getUserByToken($token);
            if(is_numeric($user_data) && $user_data < 0){
                $response = Lang::get("error.user_token_not_exist_1004");
                break;
            }

            //获取广告信息
            $ads = MarketAds::where('type',$type)
                ->where('status',1)
                ->where('city',$city)
                ->where('start_date','<',$now)
                ->where('stop_date','>',$now)
                ->select(array('id','name','desc','icon','url'))
                ->orderBy('sort', 'asc')
                ->get()->toArray();
            $count = count($ads);
            $result = array(
                'count' => $count,
                'ads' => $ads
            );
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "wxw" ,"event" => "ad", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}